<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/8
 * Time: 11:05
 */

namespace app\admin\helper;


use app\admin\model\novel\NovelM;
use app\admin\model\novel\NovelVolumeM;
use app\admin\model\TagM;
use think\Config;

class SearchHelper
{

    /**
     * 获取相关小说卷列表
     * @return mixed
     */
    static public function getVolumeList($nove_id = 0){
        $volumeM = new NovelVolumeM();
        $data = $volumeM->listAll($nove_id)->toArray();
        return LangHelper::convertLang(array_column($data,'title','id'),'field');
    }

    /**
     * 获取小说列表
     * @return mixed
     */
    static public function getNovelList($type = 'all'){
        $novelM = new NovelM();
        $data = $novelM->listAll()->toArray();
        if ($type == 'all') {
            $list[0] = '__all__';
            $list = $list + array_column($data,'title','id');
        }else {
            $list = array_column($data,'title','id');
        }
        return LangHelper::convertLang($list,'field');
    }

    /**
     * 获取文章动态分类
     * @return mixed
     */
    static public function getTagList(){
        $tag = array_keys(Config::get('app.tag.category'))[2];
        $tagM = new TagM();
        $data = $tagM->listAll($tag)->toArray();
        $list[0] = '__all__';
        $list = array_merge($list,array_column($data,'name','code'));
        return LangHelper::convertLang($list,'field');
    }

    /**
     * 获取配置分类模块
     * @return mixed
     */
    static public function getCategoryList(){
        $list[0] = '__all__';
        $list = array_merge($list,Config::get('app.tag.category'));
        return LangHelper::convertLang($list,'field');
    }

    /**
     * 1：下架 2：准备中 3：连载  4：完结
     * @return mixed
     */
    static public function getNovelStatusList(){
        $list = [
            0=>'__all__',
            1=>'__ots__',
            2=>'__prep__',
            3=>'__serialize__',
            4=>'__over__',
        ];
        return LangHelper::convertLang($list,'field');
    }

    /**
     * 通用状态
     * @return mixed
     */
    static public function getStatusList(){
        $list = [
            0=>'__all__',
            1=>'__on__',
            2=>'__off__',
        ];
        return LangHelper::convertLang($list,'field');
    }

    /**
     * 返回当月区间数据
     * @return string
     */
    static public function getMonthDateRange(){
        return date("2018-01-01", time())." - ".date("Y-m-d", time());
    }

    /**
     * 返回当天区间段
     * @return string
     */
    static public function getDateRange(){
        return date("Y-m-d")." - ".date("Y-m-d", time());
    }

    /**
     * 返回单天时间
     * @return false|string
     */
    static public function getNowDate(){
        return date("Y-m-d", time());
    }


    static public function convertValue(&$search){
        //填充value
        foreach ($search as $k=>$v) {
            //存在需要动态生成的表单value
            if (isset($v['value']) && $k!='button' && strpos($v['value'],'function:') !== false ){
                $function_name = explode('function:',$v['value'])[1];
                if(strpos($function_name,'(') !== false){
                    $params = explode(',',explode(')',explode('(',$function_name)[1])[0]);
                    $reParams = [];
                    foreach ($params as $v1){
                        //判断是否动态变量
                        if (strpos($v1,'__') !== false && substr_count($v1,'__') == 2) {
                            $v_tmp = str_replace('__','',$v1);
                            //获取动态变量值
                            if (!empty($search[$v_tmp]['value'])) {
                                $v_tmp = array_keys($search[$v_tmp]['value'])[0];
                                $reParams[] = $v_tmp;
                            }
                        }else {
                            $reParams[] = $v1;
                        }
                    }
                    $value = call_user_func_array('\app\admin\helper\SearchHelper::'.explode('(',$function_name)[0],$params);
                }else{
                    $value = call_user_func_array('\app\admin\helper\SearchHelper::'.$function_name,[0]);
                }
                $search[$k]['value'] = $value;
            }
        }
    }

}